package com.ry.novel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.model.dto.CategoryInfo;
import com.ry.novel.model.dto.condition.CategoryQuery;
import com.ry.novel.model.resp.Result;
import com.ry.novel.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ry
 * @since 2022-04-09 16:58
 **/
@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @PostMapping("/queryCategory")
    public Result<Page<CategoryInfo>> queryCategory(@RequestBody CategoryQuery categoryQuery){
        Page<CategoryInfo> categoryInfoPage = categoryService.queryCategory(categoryQuery);

        return Result.ok(categoryInfoPage);
    }

    @PostMapping("/addCategory")
    public Result<Void> addCategory(@RequestBody CategoryInfo categoryInfo){
        categoryService.addCategory(categoryInfo);

        return Result.ok();
    }

    @PostMapping("/updateCategory")
    public Result<Void> updateCategory(@RequestBody CategoryInfo categoryInfo){
        categoryService.updateCategory(categoryInfo);

        return Result.ok();
    }

    @PostMapping("/deleteCategory")
    public Result<Void> deleteCategory(@RequestParam("id")Long id){
        categoryService.deleteCategory(id);

        return Result.ok();
    }

    @GetMapping("/queryAllCategory")
    public Result<List<CategoryInfo>> queryAllCategory(){
        return Result.ok(categoryService.queryAllCategory());
    }
}
